package cn.yuxiaoyuan.controller;

import cn.yuxiaoyuan.article.doamin.Comment;
import cn.yuxiaoyuan.common.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author solargen
 * @version 1.0
 * @date 2025/5/17 14:43
 */
@RestController
@RequestMapping("/commont")
public class CommentController {
    @Autowired
    private MongoTemplate mongoTemplate;

    @GetMapping("/all/{articleId}")
    public R all(@PathVariable("articleId")Long articleId){
        Query query = new Query(Criteria.where("articleId").is(articleId));
        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        return R.ok(comments);
    }
    @PostMapping
    public R common(@RequestBody Comment comment){
        mongoTemplate.insert(comment);
        return R.ok();
    }

    //评论的id
    @GetMapping("/good/{id}")
    public R good(@PathVariable("id")Long id){
        Comment comment = mongoTemplate.findById(id, Comment.class);
        comment.setLikenum(comment.getLikenum()+1);
        mongoTemplate.save(comment);
        return R.ok();
    }
//    @GetMapping("/noGood/{articleId}")
//    public R noGood(@PathVariable("articleId")Long articleId){
//        Comment comment = mongoTemplate.findById(articleId, Comment.class);
//        comment.setLikenum(comment.getLikenum()-1);
//        mongoTemplate.save(comment);
//        return R.ok();
//    }
}
